<?php

require_once WACT_ROOT . 'controller/controller.inc.php';
require_once WACT_ROOT . 'controller/form.inc.php';
require_once APP_ROOT . 'model/common/date.php';

// administrator user create/update form
class RegisteredUsersFormController extends FormController {
	 function RegisteredUsersFormController() {
     	$this->addChild('create', new ButtonController(new Delegate($this, 'create'))); // create user
        $this->addChild('save', new ButtonController(new Delegate($this, 'save'))); // save user
        $this->addChild('delete', new ButtonController(new Delegate($this, 'delete'))); // delete user
        $this->addChild('deactivate', new ButtonController(new Delegate($this, 'deactivate'))); // deactivate user
        $this->addChild('activate', new ButtonController(new Delegate($this, 'activate'))); // activate user
        $this->addChild('clean', new ButtonController(new Delegate($this, 'clean'))); // clean user
        $this->addChild('users', new ButtonController(new Delegate($this, 'clean'))); // clean user

        $this->setDefaultChild('clean');

		$this->addRule(new Handle('RequiredRule', array('login')));
		$this->addRule(new Handle('RequiredRule', array('password')));
        $this->addRule(new Handle('RequiredRule', array('email')));

		$this->addRule(new Handle('SizeRangeRule', array('login', 32)));

        $this->registerOnLoadListener(new Delegate($this, 'load'));
        $this->registerOnActivateListener(new Delegate($this, 'guard'));

        $this->addView('delete', new Handle('RedirectView', array('/delete')));

	 }

	 function delete(&$source, &$request, &$responseModel) {
	 	return 'delete';
	 }

	 function activate(&$source, &$request, &$responseModel) {
	 	$user_id = $request->getParameter('user_id');
	 	if ($user_id) User::activate($user_id, $responseModel, true);
	 	$responseModel->set('active_show', true);
	 	return 'success';
	 }

     function deactivate(&$source, &$request, &$responseModel) {
	 	$user_id = $request->getParameter('user_id');
	 	//print 'Deactivating user:'.$user_id;
	 	if ($user_id) User::activate($user_id, $responseModel, false);
	 	$responseModel->set('active_show', false);
	 	return 'success';
	 }

     function create(&$source, &$request, &$responseModel) {
     	if ($responseModel->isValid()) {
            require_once WACT_ROOT.'/util/phpcompat/clone.php';

            $responseModel->set('level', 0);
            $responseModel->set('active', 1);
            //$responseModel->set('reg_date', date("Y-m-d") );
            $tz =& new MyTimeZone();
            $responseModel->set('reg_date', gmdate("Y-m-d H:i", $tz->timeNowLocal() ) );
            $responseModel->set('staff', 0);
            $user_id = User::insertRegisteredUser($responseModel);
            if ($user_id) {
            	$responseModel->set('user_id', $user_id);
            	$login = $responseModel->get('login');
                $responseModel->set('login', $login);
                $lastRecord = clone_obj($responseModel);


                // Set up the model for adding the next record
                $responseModel->removeAll();
                $responseModel->set('LastRecord', $lastRecord);

                //print 'user-id='.$user_id." login = ".$login;

                return 'success';
            } else {
                return 'failure';
            }
        } else {
            //$responseModel->set('ShowPreview', TRUE);
        }
     }

     function save(&$source, &$request, &$responseModel) {
        if ($responseModel->isValid()) {
        	$user_id = $request->getParameter('user_id');
        	//print 'from save: user-id='.$user_id;

            //$responseModel->set('level', 2);
            //$responseModel->set('active', 1);
            //$responseModel->set('reg_date', date("Y-m-d") );
            //$responseModel->set('staff', 1);
            if ($user_id) User::updateRegisteredUser($user_id, $responseModel);
            return 'success';
        }
    }

    function clean(&$source, &$request, &$responseModel) {
        $responseModel->removeAll();
        $responseModel->set('supress_errors', 1);
    	return 'success';
    }

     function load(&$source, &$request, &$responseModel) {
     	$user_id = $request->getParameter('user_id');
     	//print 'from load: user-id='.$user_id;
     	if ($user_id) {
        	$record =& User::getRecord($user_id);
        	if (is_null($record)) {
            	return 'notfound';
        	}
            $responseModel->import($record->export());

        }
     }

     function guard(&$source, &$request, &$responseModel) {

     }
}

?>